package com.momo.background.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.momo.background.dto.UserInfoDto;
import com.momo.background.service.HotelInfoService;
import com.momo.background.service.UserService;
import com.momo.background.vo.*;


import com.momo.hotel.dao.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = "/background")
public class UserInfoController {
    @Autowired
    UserService userService;
    @Resource
    HttpSession session;

    @RequestMapping(value = "/BackgroundLogin", method = RequestMethod.POST)
    @ResponseBody
    public Object BackgroundLogin(@RequestBody(required = false) AdminVo adminVo, HttpServletRequest request) {
        HttpSession session = request.getSession();
        AdminVo adminVo1 = userService.AdminLogin(adminVo);
        System.out.println(adminVo1);
        session.setAttribute("UserInfo", adminVo1);
        if (adminVo1 != null) {
            return true;
        }
        return false;
    }

    @RequestMapping(value = "/ListUser",method = RequestMethod.POST)
    public Object SelectAll(@RequestBody(required = false) UserInfoDto userInfoDto){
        PageHelper.startPage(userInfoDto.getCurrentPage(),userInfoDto.getPageSize());
        List<UserInfoDto> userInfoDtos = userService.ListAll(userInfoDto);
        PageInfo<UserInfoDto> pageInfo = new PageInfo<UserInfoDto>(userInfoDtos);
        System.out.println(pageInfo.getList());
        return pageInfo;
    }

    @RequestMapping(value = "/GetSessionLoginInfo",method = RequestMethod.GET)
    public Object GetSessionLoginInfo(HttpSession session){
        return session.getAttribute("UserInfo");
    }

    @RequestMapping(value = "/delSessionLoginInfo",method = RequestMethod.GET)
    public Boolean delSessionLoginInfo(HttpServletRequest request){
        try{
            request.getSession().setAttribute("UserInfo",null);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @RequestMapping(value = "/hotelUserRegister",method = RequestMethod.POST)
    @ResponseBody
    public Boolean userRegister(@RequestBody AdminVo adminVo){
        int rSet=userService.userRegister(adminVo);
        if (rSet!=0){
            return true;
        }
        return false;
    }
    @RequestMapping(value = "/hotelPhoneCheck",method = RequestMethod.POST)
    @ResponseBody
    public Boolean hotelPhoneCheck(@RequestBody String phone){
        System.out.println(phone.substring(6));
        AdminVo adminVo=userService.hotelPhoneCheck(phone.substring(6));
        if (adminVo!=null){
            return false;
        }
        return true;
    }

    @RequestMapping(value = "/hotelUserNameCheck",method = RequestMethod.POST)
    @ResponseBody
    public Boolean hotelUserNameCheck(@RequestBody String username){
        System.out.println(username.substring(9));
        AdminVo adminVo=userService.hotelUserNameCheck(username.substring(9));
        if (adminVo!=null){
            return false;
        }
        return true;
    }

    @RequestMapping(value = "/GetUserByID",method = RequestMethod.POST)
    public TbUser GetUserByID(@RequestBody UserInfoDto userInfoDto){
        Integer id = userInfoDto.getId();
        return userService.SelectUserByID(id).get(0);
    }

    @RequestMapping(value = "/UpdateUserByID",method = RequestMethod.POST)
    public String UpdateUserByID(@RequestBody UserVo userVo){
        Boolean aBoolean = userService.updateUserInfoById(userVo);
        return aBoolean.toString();
    }

    @RequestMapping(value = "/delUser",method = RequestMethod.POST)
    @ResponseBody
    public Boolean delUser(@RequestBody IdVo idVo){
        String id = idVo.getId();
        int rSet=userService.hotelUserDel(id);
        if (rSet!=0){
            return true;
        }
        return false;
    }


    @RequestMapping(value = "/delUserByIds",method = RequestMethod.POST)
    @ResponseBody
    public Boolean delUserByIds(@RequestBody IdVo idVo){
        String[] id = idVo.getIds();
        System.out.println(id+"+++++++++++");
        int rSet=userService.hotelUserDelByIds(id);
        if (rSet==id.length){
            return true;
        }
        return false;
    }

    @Resource
    HotelInfoService hotelInfoService;

    @RequestMapping(value = "/getHotelInfo",method = RequestMethod.POST)
    @ResponseBody
    public HotelBrankVo getHotelInfo(HttpServletRequest request){
        AdminVo adminVo = (AdminVo) request.getSession().getAttribute("UserInfo");
        String hotelId = adminVo.getHotelId();
        HotelBrankVo hotelBrankVo=hotelInfoService.getHotelInfo(hotelId);
        if (hotelBrankVo!=null){
            return hotelBrankVo;
        }
        return null;
    }

    @RequestMapping(value = "/hotelInfoSave",method = RequestMethod.POST)
    @ResponseBody
    public boolean hotelInfoSave(@RequestBody HotelBrankVo hotelBrankVo,HttpServletRequest request){
       AdminVo adminVo = (AdminVo) request.getSession().getAttribute("UserInfo");
       String hotelId = adminVo.getHotelId();
       hotelBrankVo.setId(hotelId);
       int rSet=hotelInfoService.hotelInfoSave(hotelBrankVo);
       if (rSet!=0){
           return true;
       }
       return false;
    }

    @RequestMapping(value = "/ListRoom",method = RequestMethod.POST)
    @ResponseBody
    public Object SelectAllRoom(@RequestBody HotelInfo hotelInfo,HttpServletRequest request){
        AdminVo adminVo = (AdminVo) request.getSession().getAttribute("UserInfo");
        String hotelId = adminVo.getHotelId();
        hotelInfo.setHotelid(hotelId);
        PageHelper.startPage(hotelInfo.getCurrentPage(),hotelInfo.getPageSize());
        List<HotelInfo> hotelInfos = hotelInfoService.SelectAllRoom(hotelInfo);
        PageInfo<HotelInfo> pageInfo=new PageInfo<HotelInfo>(hotelInfos);
        System.out.println(pageInfo.getList());
        return pageInfo;
    }


    @RequestMapping(value = "/GetRoomByroomID",method = RequestMethod.POST)
    @ResponseBody
    public HotelInfo GetRoomByroomID(@RequestBody HotelInfo hotelInfo,HttpServletRequest request){
        AdminVo adminVo = (AdminVo) request.getSession().getAttribute("UserInfo");
        String hotelId = adminVo.getHotelId();
        hotelInfo.setHotelid(hotelId);
        HotelInfo hotelInfo1=hotelInfoService.GetRoomByroomID(hotelInfo);
        if (hotelInfo1!=null){
            return hotelInfo1;
        }
        return null;
    }

    @RequestMapping(value = "/roomInfoUpdate",method = RequestMethod.POST)
    public boolean roomInfoUpdate(@RequestBody HotelInfo hotelInfo,HttpServletRequest request){
        AdminVo adminVo = (AdminVo) request.getSession().getAttribute("UserInfo");
        String hotelId = adminVo.getHotelId();
        hotelInfo.setHotelid(hotelId);
        int rSet=hotelInfoService.roomInfoUpdate(hotelInfo);
        if (rSet==2){
            return true;
        }
        return false;
    }
}
